﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace Ryder_US_Website.BUS
{
    public class CargoStatusBUS
    {
        public static string conString = ConfigurationManager.ConnectionStrings["breakers_RyderUSConnectionString"].ConnectionString;

        #region Select
        ///<summary>
        /// Get all CargoStatus
        /// </summary>
        /// <returns>List of CargoStatus</returns>
        public static List<RUS_CargoStatus> GetAll()
        {
            List<RUS_CargoStatus> list = new List<RUS_CargoStatus>();
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                var query = from cs in context.RUS_CargoStatus
                            orderby cs.CargoStatus_name ascending
                            select cs;
                list = query.ToList<RUS_CargoStatus>();
            }
            return list;
        }

        ///<summary>
        /// Get all and paging
        /// </summary>
        /// <param name="curPage">The current page. </param>
        /// <param name="rowsPerPage"> The rows per page.</param>
        /// <param name="numPages"> The number of page. </param>
        /// <returns></returns>
        public static List<RUS_CargoStatus> GetAll(int curPage, int rowsPerPage, ref int numPages)
        {
            if (curPage < 1)
                curPage = 1;
            if (rowsPerPage < 1)
                rowsPerPage = 10;
            List<RUS_CargoStatus> list = new List<RUS_CargoStatus>();
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                var query = from cs in context.RUS_CargoStatus
                            orderby cs.CargoStatus_name ascending
                            select cs;
                numPages = (query.Count() / rowsPerPage) + 1;
                query = query.OrderBy(cs => cs.CargoStatus_name);
                list = query.Skip((curPage - 1) * rowsPerPage).Take(rowsPerPage).ToList<RUS_CargoStatus>();
            }
            return list;
        }

        /// <summary>
        /// Gets the cargostatus by ID.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        public static RUS_CargoStatus GetCargoStatusByID(int id)
        {
            RUS_CargoStatus cs = new RUS_CargoStatus();
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                var query = from c in context.RUS_CargoStatus
                            where (c.CargoStatus_ID == id)
                            select c;
                cs = query.SingleOrDefault<RUS_CargoStatus>();
            }
            return cs;
        }

        /// <summary>
        /// Gets the active cargoStatus.
        /// </summary>
        /// <returns></returns>
        public static List<RUS_CargoStatus> GetActiveCargoStatus()
        {
            List<RUS_CargoStatus> list = new List<RUS_CargoStatus>();
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                var query = from cs in context.RUS_CargoStatus
                            where (cs.CargoStatus_trash == 0)
                            orderby cs.CargoStatus_name
                            select cs;
                list = query.ToList<RUS_CargoStatus>();
            }
            return list;
        }

        /// <summary>
        /// Gets the active CargoStatus by ID.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        public static RUS_CargoStatus GetActiveCargoStatusByID(int id)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                var query = from cs in context.RUS_CargoStatus
                            where (cs.CargoStatus_ID == id && cs.CargoStatus_trash == 0)
                            select cs;
                return query.SingleOrDefault<RUS_CargoStatus>();
            }
        }

        ///<summary>
        /// Get the active cargostatus.
        /// </summary>
        /// <param name="curPage">The current page.</param>
        /// <param name="rowsPerPage">The number of row. Rows per page.</param>
        /// <param name="numPages">The number of pages.</param>
        /// <returns></returns>
        public static List<RUS_CargoStatus> GetActiveCargoStatus(int curPage, int rowsPerPage, ref int numPages)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                if (curPage < 1)
                    curPage = 1;
                if (rowsPerPage < 1)
                    rowsPerPage = 10;
                List<RUS_CargoStatus> list = new List<RUS_CargoStatus>();
                var query = from cs in context.RUS_CargoStatus
                            where (cs.CargoStatus_trash == 0)
                            orderby cs.CargoStatus_name
                            select cs;
                numPages = (query.Count() / rowsPerPage) + 1;
                list = query.Skip((curPage - 1) * rowsPerPage).Take(rowsPerPage).ToList<RUS_CargoStatus>();
                return list;
            }
        }

        /// <summary>
        /// Gets the inactive cargoStatus.
        /// </summary>
        /// <returns></returns>
        public static List<RUS_CargoStatus> GetInactiveCargoStatus()
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                List<RUS_CargoStatus> list = new List<RUS_CargoStatus>();
                var query = from s in context.RUS_CargoStatus
                            where (s.CargoStatus_trash == 1)
                            orderby s.CargoStatus_name
                            select s;
                list = query.ToList<RUS_CargoStatus>();
                return list;
            }
        }

        /// <summary>
        /// Gets the inactive cargoStatus.
        /// </summary>
        /// <param name="curPage">The current page.</param>
        /// <param name="rowsPerPage">The number of rows. Rows per page.</param>
        /// <param name="numPages">The number of pages.</param>
        /// <returns></returns>
        public static List<RUS_CargoStatus> GetInactiveCargoStatus(int curPage, int rowsPerPage, ref int numPages)
        {
            if (curPage < 1)
                curPage = 1;
            if (rowsPerPage < 1)
                rowsPerPage = 10;
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                List<RUS_CargoStatus> list = new List<RUS_CargoStatus>();
                var query = from s in context.RUS_CargoStatus
                            where (s.CargoStatus_trash == 1)
                            orderby s.CargoStatus_name
                            select s;
                numPages = (query.Count() / rowsPerPage) + 1;
                list = query.Skip((curPage - 1) * rowsPerPage).Take(rowsPerPage).ToList<RUS_CargoStatus>();
                return list;
            }
        }

        #endregion

        #region Insert
        /// <summary>
        /// Inserts the specified cargoStatus.
        /// </summary>
        /// <param name="cargoStatus">The cargostatus.</param>
        public static void Insert(RUS_CargoStatus cargoStatus)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                if (cargoStatus != null)
                    context.RUS_CargoStatus.InsertOnSubmit(cargoStatus);
                try
                {
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw new Exception("Không thể thêm trạng thái đơn hàng được.");
                }
            }
        }

        ///<summary>
        /// Insert specified name
        /// </summary>
        /// <param name="name">The cargostatus's name</param>
        /// <param name="description">The cargostatus's description</param>
        public static void Insert(String name, String description)
        {
            if (String.IsNullOrEmpty(name))
                throw new Exception("CARGOSTATUS_NAME_CANOT_BE_EMTY");
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                RUS_CargoStatus cargoStatus = new RUS_CargoStatus();
                cargoStatus.CargoStatus_name = name;
                cargoStatus.CargoStatus_description = description;
                cargoStatus.CargoStatus_trash = 0;
                context.RUS_CargoStatus.InsertOnSubmit(cargoStatus);
                try
                {
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw new Exception("CANOT_INSERT_CARGOSTATUS");
                }
            }
        }
        #endregion

        #region Update
        ///<summary>
        /// Update the specified cargoStatus 
        /// </summary>
        /// <param name="cargoStatus"> The CargoStatus</param>
        public static void Update(RUS_CargoStatus cargoStatus)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                try
                {
                    if (cargoStatus != null)
                        context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw new Exception("Có lỗi xảy ra! Không thể phục hồi dịch vụ được.");
                }
            }
        }

        ///<summary>
        /// Update the specified cargoStatus 
        /// </summary>
        /// <param name="name">The cargoStatus's name</param>
        /// <param name="description">The cargoStatus's description</param>
        /// <param name="id">The id of cargoStatus need updating</param>
        public static RUS_CargoStatus Update(int id, String name, String description, int trash)
        {
            RUS_CargoStatus cargoStatus = null;
            if (String.IsNullOrEmpty(name))
                throw new Exception("Tên trạng thái đơn hàng không được để trống.");
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                var query = from cs in context.RUS_CargoStatus
                            where (cs.CargoStatus_ID == id)
                            select cs;
                cargoStatus = query.SingleOrDefault<RUS_CargoStatus>();
                if (cargoStatus != null)
                {
                    cargoStatus.CargoStatus_name = name;
                    cargoStatus.CargoStatus_description = description;
                    cargoStatus.CargoStatus_trash = trash;
                }
                try
                {
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw new Exception("Có lỗi xảy ra! Không thể phục hồi dịch vụ được.");
                }
            }
            //var query = from cs in context.RUS_CargoStatus
            //            where (cs.CargoStatus_ID == id)
            //            select cs;
            //RUS_CargoStatus cargoStatus = query.SingleOrDefault<RUS_CargoStatus>();
            //if (cargoStatus != null)
            //{
            //    cargoStatus.CargoStatus_name = name;
            //    cargoStatus.CargoStatus_description = description;
            //    cargoStatus.CargoStatus_trash = trash;
            //}
            //try
            //{
            //    context.SubmitChanges();
            //}
            //catch(Exception ex)
            //{
            //    Console.WriteLine(ex.Message);
            //    throw new Exception("Có lỗi xảy ra! Không thể phục hồi dịch vụ được.");
            //}
            return cargoStatus;
        }
        #endregion

        #region Delete & Restore
        ///<summary>
        /// Delete specified cargoStatus
        /// </summary>
        /// <param name="cargoStatus">The cargoStatus.</param>
        public static RUS_CargoStatus Delete(RUS_CargoStatus cargoStatus)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                foreach (RUS_Cargo cg in cargoStatus.RUS_Cargos)
                {
                    if (cg.Cargo_trash == 0)
                        throw new Exception("Không thể xóa vì tồn tại dữ liệu liên quan.");
                }
                cargoStatus.CargoStatus_trash = 1;
                try
                {
                    context.RUS_CargoStatus.Attach(cargoStatus);
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw new Exception("CANNOT_DELETE_CARGOSTATUS");
                }
                return cargoStatus;
            }
        }

        /// <summary>
        /// Restore the specified cargoStatus.
        /// </summary>
        /// <param name="cargoStatus">The cargoStatus.</param>
        public static RUS_CargoStatus Restore(RUS_CargoStatus cargoStatus)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                if (cargoStatus == null)
                    return null;
                cargoStatus.CargoStatus_trash = 0;
                try
                {
                    context.RUS_CargoStatus.Attach(cargoStatus);
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw new Exception("CANNOT_RESTORE_CARGOSTATUS");
                }
                return cargoStatus;
            }
        }

        /// <summary>
        /// Delete the specified cargoStatus by ID.
        /// </summary>
        /// <param name="id">The cargoStatus's id.</param>
        public static RUS_CargoStatus Delete(int id)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                var query = from cs in context.RUS_CargoStatus
                            where (cs.CargoStatus_ID == id)
                            select cs;
                RUS_CargoStatus cargoStatus = query.SingleOrDefault<RUS_CargoStatus>();
                foreach (RUS_Cargo cg in cargoStatus.RUS_Cargos)
                {
                    if (cg.Cargo_trash == 0)
                        throw new Exception("Không thể xóa vì tồn tại dữ liệu liên quan.");
                }
                cargoStatus.CargoStatus_trash = 1;
                try
                {
                    context.RUS_CargoStatus.Attach(cargoStatus);
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw new Exception("CANNOT_DELETE_CARGOSTATUS");
                }
                return cargoStatus;
            }
        }

        /// <summary>
        /// Restores the specified cargoStatus by ID.
        /// </summary>
        /// <param name="id">The cargoStatus's id.</param>
        public static RUS_CargoStatus Restore(int id)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                var query = from cs in context.RUS_CargoStatus
                            where (cs.CargoStatus_ID == id)
                            select cs;
                RUS_CargoStatus cargoStatus = query.SingleOrDefault<RUS_CargoStatus>();
                cargoStatus.CargoStatus_trash = 0;
                try
                {
                    context.RUS_CargoStatus.Attach(cargoStatus);
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw new Exception("CANNOT_RESTORE_CARGOSTATUS");
                }
                return cargoStatus;
            }
        }

        /// <summary>
        /// Deletes multiple specified cargoStatus.
        /// </summary>
        /// <param name="ids">The cargoStatus' ids.</param>
        public static List<RUS_CargoStatus> Delete(int[] ids)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                var query = from cs in context.RUS_CargoStatus
                            where (ids.Contains(cs.CargoStatus_ID))
                            select cs;
                List<RUS_CargoStatus> list = query.ToList<RUS_CargoStatus>();
                foreach (RUS_CargoStatus cs in list)
                {
                    foreach (RUS_Cargo sd in cs.RUS_Cargos)
                        if (sd.Cargo_trash == 0)
                            throw new Exception("Không thể xóa vì tồn tại dữ liệu liên quan.");
                    cs.CargoStatus_trash = 1;
                }
                try
                {
                    context.RUS_CargoStatus.AttachAll<RUS_CargoStatus>(list);
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw new Exception("CANNOT_DELETE_CARGOSTATUS");
                }
                return list;
            }
        }

        /// <summary>
        /// Restores multiple specified cargoStatus.
        /// </summary>
        /// <param name="ids">The ids.</param>
        public static List<RUS_CargoStatus> Restore(int[] ids)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                var query = from cs in context.RUS_CargoStatus
                            where (ids.Contains(cs.CargoStatus_ID))
                            select cs;
                List<RUS_CargoStatus> list = query.ToList<RUS_CargoStatus>();
                foreach (RUS_CargoStatus cs in list)
                {
                    cs.CargoStatus_trash = 0;
                }
                try
                {
                    context.RUS_CargoStatus.AttachAll<RUS_CargoStatus>(list);
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw new Exception("CANNOT_RESTORE_CARGOSTATUS");
                }
                return list;
            }
        }

        /// <summary>
        /// Deletes multiples specified cargoStatus.
        /// </summary>
        /// <param name="cargoStatuses">The cargoStatuses.</param>
        public static List<RUS_CargoStatus> Delete(List<RUS_CargoStatus> cargoStatuses)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                foreach (RUS_CargoStatus cs in cargoStatuses)
                {
                    foreach (RUS_Cargo cg in cs.RUS_Cargos)
                    {
                        if (cg.Cargo_trash == 0)
                            throw new Exception("Không thể xóa vì tồn tại dữ liệu liên quan.");
                        cs.CargoStatus_trash = 1;
                    }
                }
                try
                {
                    context.RUS_CargoStatus.AttachAll<RUS_CargoStatus>(cargoStatuses);
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw new Exception("CANNOT_DELETE_CARGOSTATUS");
                }
                return cargoStatuses;
            }
        }

        /// <summary>
        /// Restores multiple specified cargoStatus.
        /// </summary>
        /// <param name="ids">The ids.</param>
        public static List<RUS_CargoStatus> Restore(List<RUS_CargoStatus> cargoStatuses)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                foreach (RUS_CargoStatus cs in cargoStatuses)
                {
                    cs.CargoStatus_trash = 0;
                }
                try
                {
                    context.RUS_CargoStatus.AttachAll<RUS_CargoStatus>(cargoStatuses);
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw new Exception("CANNOT_RESTORE_CARGOSTATUS");
                }
                return cargoStatuses;
            }
        }

        /// <summary>
        /// Deletes or restores CargoStatus.
        /// </summary>
        /// <param name="cargoStatus">The cargoStatus.</param>
        public static RUS_CargoStatus DeleteRestore(RUS_CargoStatus cargoStatus)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                if (cargoStatus.CargoStatus_trash == 0)
                {
                    foreach (RUS_Cargo cg in cargoStatus.RUS_Cargos)
                    {
                        if (cg.Cargo_trash == 0)
                            throw new Exception("Không thể xóa vì tồn tại dữ liệu liên quan.");
                    }
                }
                cargoStatus.CargoStatus_trash = 1 - cargoStatus.CargoStatus_trash;
                try
                {
                    context.RUS_CargoStatus.Attach(cargoStatus);
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw new Exception("CANNOT_RESTORE_CARGOSTATUS");
                }
                return cargoStatus;
            }
        }

        /// <summary>
        /// Deletes or restores CargoStatus.
        /// </summary>
        /// <param name="id">The id.</param>
        public static RUS_CargoStatus DeleteRestore(int id)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                var query = from cs in context.RUS_CargoStatus
                            where (cs.CargoStatus_ID == id)
                            select cs;
                RUS_CargoStatus cargoStatus = query.SingleOrDefault<RUS_CargoStatus>();
                if (cargoStatus.CargoStatus_trash == 0)
                {
                    foreach (RUS_Cargo cg in cargoStatus.RUS_Cargos)
                    {
                        if (cg.Cargo_trash == 0)
                        {
                            throw new Exception("Không thể xóa vì tồn tại dữ liệu liên quan.");
                        }

                    }
                }
                cargoStatus.CargoStatus_trash = 1 - cargoStatus.CargoStatus_trash;
                try
                {
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw new Exception("Không thể phục hồi tình trạng đơn hàng");
                }
                return cargoStatus;
            }
        }
        #endregion
    }

}
